package com.bosssoft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.baseParam.BaseParam;
import com.bosssoft.entity.dto.FibSealDTO;
import com.bosssoft.entity.dto.FibSealDeptDTO;
import com.bosssoft.entity.po.FibSealDeptPO;
import com.bosssoft.entity.vo.FibSealDeptVO;
import com.bosssoft.entity.vo.FibSealVO;
import com.bosssoft.exception.DaoException;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibSealDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Aria
 */
@Api(tags = "印章归属表")
@RequestMapping("/sealDept")
@RestController
public class FibSealDeptController {

    @Autowired
    FibSealDeptService sealDeptService;

    @ApiOperation(value = "分页查询")
    @PostMapping("/getSealDeptPage")
    public Result getSealPage(@RequestParam(defaultValue = "1") Integer pageNo,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Page<FibSealDeptPO> page = new Page<>(pageNo, pageSize);
            Page<FibSealDeptPO> sealDeptPage = sealDeptService.page(page);
            Long total = sealDeptPage.getTotal();
            List<FibSealDeptPO> records = sealDeptPage.getRecords();
            PageResult<FibSealDeptPO> fibSealDeptPOPageResult = new PageResult<>(page, records);
            return Result.ok().data("page", fibSealDeptPOPageResult);
        } catch (DaoException e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "新增或更新,不传id是新增,传id是更新")
    @PostMapping("/sealDeptInsertOrUpdate")
    public Result sealInsertOrUpdate(@RequestBody @Validated({BaseParam.add.class}) FibSealDeptVO fibSealDeptVO) {
        FibSealDeptDTO fibSealDeptDTO = new FibSealDeptDTO();
        BeanUtils.copyProperties(fibSealDeptVO, fibSealDeptDTO);
        try {
            sealDeptService.insertOrUpdate(fibSealDeptDTO);
        } catch (DaoException e) {
            if (e.getMessage() != null) {
                return Result.error(e.getCode(), e.getMessage());
            }
            return Result.error();
        }
        return Result.ok();
    }

    @ApiOperation(value = "根据id删除")
    @PostMapping("/sealDeptDelete")
    public Result sealDelete(@RequestBody @Validated({BaseParam.delete.class}) FibSealDeptVO fibSealDeptVO) {
        try {
            FibSealDeptDTO fibSealDeptDTO = new FibSealDeptDTO();
            sealDeptService.updateDeleted(fibSealDeptDTO);
        } catch (DaoException e) {
            if (e.getMessage() != null) {
                return Result.error(e.getCode(), e.getMessage());
            }
            return Result.error();
        }
        return Result.ok();
    }

    @ApiOperation(value = "批量删除")
    @PostMapping("/sealDeptDeleteList")
    public Result sealDeleteList(@RequestBody List<FibSealDeptVO> fibSealVOList) {
        try {
            sealDeptService.sealDeleteList(fibSealVOList);
        } catch (DaoException e) {
            if (e.getMessage() != null) {
                return Result.error(e.getCode(), e.getMessage());
            }
            return Result.error();
        }
        return Result.ok();
    }

    @ApiOperation(value = "查找一个部门的所有印章_根据部门id")
    @GetMapping("/getSealBySealId")
    public Result getSealBySealId(@RequestParam Long deptId) {
        try {
            List<FibSealDeptPO> records = sealDeptService.getSealBySealId(deptId);
            return Result.ok().data("list", records);
        } catch (DaoException e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "查询所有数据")
    @PostMapping("/getSealAll")
    public Result getSealAll() {
        try {
            List<FibSealDeptPO> records = sealDeptService.list();
            return Result.ok().data("list", records);
        } catch (DaoException e) {
            e.printStackTrace();
            return Result.error();
        }
    }

}
